<?php

namespace App\library\services;

use App\Models\UserRelease;
use App\Models\UserReleaseImages;
use Illuminate\Support\Facades\DB;

class UserReleaseService extends BaseServices
{
    /**
     * Author:Zhongyuan Lu
     * 发布记录入库
     * @param $params
     * @return array
     */
    public static function createRelease($params)
    {
        //发布记录入库
        $id = UserRelease::create($params)->id;
        if ($id) {
            return self::success('发布成功', $id);
        }
        return self::fail('发布失败');
    }

    /**
     * Author:Zhongyuan Lu
     * 发布记录图片入库
     * @param $releaseId
     * @param $params
     */
    public static function createReleaseImage($releaseId, $params)
    {
        $releaseImages = [];
        //室内图片
        if (isset($params['indoor_images']) && !empty($params['indoor_images'])) {
            $releaseImages['indoor_images'] = explode('|', $params['indoor_images']);
        }
        //户型图片
        if (isset($params['house_type_images']) && !empty($params['house_type_images'])) {
            $releaseImages['house_type_images'] = explode('|', $params['house_type_images']);
        }
        //室外图片
        if (isset($params['outdoor_images']) && !empty($params['outdoor_images'])) {
            $releaseImages['outdoor_images'] = explode('|', $params['outdoor_images']);
        }

        $releaseImages['release_id'] = $releaseId;
        //发布记录的图片入库
        $id = UserReleaseImages::create($releaseImages)->id;
        if ($id) {
            return self::success('图片入库成功', $id);
        }
        return self::fail('图片入库失败');
    }

    /**
     * Author:Zhongyuan Lu
     * 审核
     * @param $id
     * @param $status
     * @return array
     */
    public static function examine($id, $status)
    {
        DB::beginTransaction();
        try {
            //发布状态修改  status 1 审核通过  0 审核中  -1 审核不通过
            $userRelease = UserRelease::where('id', $id)->first();
            if ($userRelease->status != 0) {
                return self::fail('无法修改状态');
            }
            $userRelease->status = $status;
            $userRelease->save();

            //审核通过  将发布信息添加到房源表中
            if ($status == 1) {
                $userRelease = UserRelease::where('id', $id)->first()->toArray();
                $userRelease['connect_name'] = $userRelease['username'];    //联系人
                $userRelease['days'] = $userRelease['effective_time'];      //有效期
                $type = [
                    1 => 2,
                    2 => 1,
                ];
                $userRelease['sale_type'] = $type[$userRelease['type']];    //售卖方式  1 租赁 2 买卖
                //创建房源
                $secondHouse = SecondHouseService::createHouse($userRelease);
                if ($secondHouse['code'] != 200) {
                    return self::fail($secondHouse['message']);
                }

                //图片绑定房源id
                $images =  UserReleaseService::updateHouse($id,$secondHouse['data']);
                if ($images['code'] != 200) {
                    return self::fail($images['message']);
                }
            }

            DB::commit();
            return self::success('修改成功');
        } catch (\Exception $exception) {
            DB::rollBack();
            return self::fail($exception->getMessage());
        }
    }

    /**
     * Author:Zhongyuan Lu
     * 绑定房源id
     * @param $releaseId
     * @param $houseId
     * @return array
     */
    public static function updateHouse($releaseId,$houseId)
    {
        $bool = UserReleaseImages::where('release_id',$releaseId)->update(['house_id'=>$houseId]);
        if ($bool) {
            return self::success('图片关联房源成功');
        }
        return self::fail('图片关联房源失败');
    }


    /**
     * Author:YangXinPeng
     * 获取用户求购列表
     * @return array
     */
    public static function askToBuy()
    {
        $data = UserRelease::with(['region','city','province'])->where('type',3)->get();
        $data && $data = $data->toArray();
        return self::success('成功',$data);
    }
}
